package com.qf.android.sport.utils.headline;

import android.databinding.BindingAdapter;
import android.support.percent.PercentRelativeLayout;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.qf.android.sport.adapter.headline.HeadlineHeadlineRecyclerAdapter;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * Created by wen10 on 2016/12/16.
 */

public class headBindings {

    private static IjkMediaPlayer player = new IjkMediaPlayer();

    @BindingAdapter("loadImg")
    public static void loadImg(ImageView imageView, String url) {
        Picasso.with(imageView.getContext())
                .load(url)
                .into(imageView);

    }


    @BindingAdapter("loadIconImg")
    public static void loadIconImg(ImageView imageView, String url) {
        Picasso.with(imageView.getContext())
                .load(url)
                .transform(new CircleTansformation())
                .into(imageView);


    }

    @BindingAdapter("dynamicLoadImg")
    public static void loadImg(SimpleDraweeView simpleDraweeView, String url) {
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .reset()
                .setUri("http://lifeixphoto.qiniudn.com/" + url)
                .setAutoPlayAnimations(true)
                .build();
        simpleDraweeView.setController(controller);

    }

    @BindingAdapter("bofang")
    public static void bofang(final ImageView imageView, final String url) {
        //布局内控件
        final PercentRelativeLayout percentRelativeLayout = (PercentRelativeLayout) imageView.getParent();
        final LinearLayout linearLayout = (LinearLayout) percentRelativeLayout.getParent();
        final RecyclerView recyclerView = (RecyclerView) linearLayout.getParent().getParent();
        final CardView cardView = (CardView) linearLayout.getParent();
        final SurfaceView surfaceView = (SurfaceView) percentRelativeLayout.getChildAt(0);
        final ImageView surfaceImg = (ImageView) percentRelativeLayout.getChildAt(1);
        final ProgressBar progressBar = (ProgressBar) percentRelativeLayout.getChildAt(2);

        surfaceView.setVisibility(View.VISIBLE);//触发surfaceView create
        SurfaceHolder holder = surfaceView.getHolder();
        surfaceView.setTag(holder);

        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(final SurfaceHolder holder) {

                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                        player.reset();
                        player.setDisplay(holder);
                        imageView.setVisibility(View.INVISIBLE);
                        progressBar.setVisibility(View.VISIBLE);

                        try {
                            player.setDataSource(url);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        player.prepareAsync();
                        player.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(IMediaPlayer iMediaPlayer) {

                                surfaceImg.setVisibility(View.INVISIBLE);
                                progressBar.setVisibility(View.INVISIBLE);
                                player.start();
                            }
                        });

                        player.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(IMediaPlayer iMediaPlayer) {
                                surfaceImg.setVisibility(View.VISIBLE);
                                imageView.setVisibility(View.VISIBLE);
                                surfaceView.setVisibility(View.GONE);
                            }
                        });

                        player.setOnBufferingUpdateListener(new IMediaPlayer.OnBufferingUpdateListener() {
                            @Override
                            public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int i) {
                                Log.d("imediaPlayer", "imediaPlayer " + i);
                            }
                        });

                    }


                });


            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

                if (player.isPlaying()) {

                    player.stop();
                }
                progressBar.setVisibility(View.GONE);
                surfaceImg.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.VISIBLE);
            }
        });

    }

    @BindingAdapter("stickyText")
    public static void loadImg(RecyclerView recyclerView, int useless) {
        RelativeLayout relativeLayout = (RelativeLayout) recyclerView.getParent();
        final TextView stickyView = (TextView) relativeLayout.getChildAt(1);
        //recyclerView的顶部悬浮视图逻辑操作
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View topView = recyclerView.findChildViewUnder(stickyView.getMeasuredWidth() / 2, 5);

                if (topView != null && topView.getContentDescription() != null) {
                    stickyView.setText(String.valueOf(topView.getContentDescription()));
                    stickyView.setVisibility(View.VISIBLE);
                }

                View transInfoView = recyclerView.findChildViewUnder(stickyView.getMeasuredWidth() / 2, stickyView.getMeasuredHeight() + 1);
                if (transInfoView != null && transInfoView.getTag() != null) {


                    int transState = (int) transInfoView.getTag();
                    int dealtY = transInfoView.getTop() - stickyView.getMeasuredHeight();
                    if (transState == HeadlineHeadlineRecyclerAdapter.HAS_STICKY_VIEW) {
                        if (transInfoView.getTop() > 0) {
                            stickyView.setTranslationY(dealtY);
                        } else {
                            stickyView.setTranslationY(0);
                        }
                    } else if (transState == HeadlineHeadlineRecyclerAdapter.NONE_STICKY_VIEW) {
                        stickyView.setTranslationY(0);
                    }

                }

            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }
        });
    }


}
